-- Usage:
-- sqlite3 MC2020_results.db < post-processing-script.txt

.echo on

--Perform this step only if the simulations were run in parallel batches.
-- In this case, MC2020_results.db should not have any results.
.read merge1_attachments.sql
.read merge2_create.sql
.read merge3_join.sql

--current_msr_mat: alternatively, copy SQL dump from the input database.
.mode csv
.import RunMeasures-BFMv3.csv current_msr_mat

--Load the residential support tables
.read peakperspec.sql
.read reststatwt.sql
.read wts_res_bldg.sql
.read wts_res_hvac.sql
.read wts_res_loc.sql
.read wts_res_vintage.sql
.read FloorArea.sql
.read NumBldgs.sql
.read NumStor.sql

--Residential post-processing through P4 (weighted average over vintages into eras).
.read R1-WtTstat-Annual.sql
.read R2-WtTstat-HourlyWB.sql
.read R3-SumTStat-Annual.sql
.read R4-SumTStat-HourlyWB.sql
.read P1-Create-sim_peakper.sql
.read P2-Calc_MsrImpacts.sql
.read P3-VintMult.sql
.read P4-SumVint.sql
.read P4-SumVint_Statewide.sql

--Export results for Excel
.mode list
.headers on
.separator ,

-- This table has raw, unweighted measure impacts, by vintage, etc, with PA="Any".
.once meas_impacts.csv
select * from meas_impacts;

--This table has had vintage weights applied to re-index: from indexing by vintage,
-- to indexing by era (old, ex, rec, new) and by specific PA for non-new eras.
-- Note that "new" is an unweighted copy of the most recent vintage prototype.
.once meas_impacts_wtd.csv
select * from meas_impacts_wtd;

--This table applies the weights AND performs the averaging over (vintage, PA)
-- combinations that match the desired indexing by era and CZ, etc.
.once meas_impacts_statewide.csv
select * from meas_impacts_statewide;
